﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cricket.LinqData;
using Cricket.LinqModel;

public partial class AddTeam : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    Team team;
    List<Team> teams;    
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //if (Session["AdminUser"] != null)
            //    SessionManager.CurrentUser = (LoggedInUser)Session["AdminUser"];
            HttpCookie cookie = Request.Cookies["Admin"];
            if (cookie == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {

                if (Request.QueryString["Id"] != null)
                {
                    fillTeam(Request.QueryString["Id"].ToString());
                }
                bindType();
            }
        }
    }

    private void bindType()
    {
        Series series = new Series(_dataObject);
        List<Series> lstSeries = new List<Series>();
        lstSeries = series.GetAllSeries();
        if (lstSeries.Count > 0)
        {
            drpType.DataSource = lstSeries;
            drpType.DataTextField = "Name";
            drpType.DataValueField = "Id";
            drpType.DataBind();
            drpType.Items.Insert(0, new ListItem("Select Type", "0"));
        }
        else
        {
            drpType.DataSource = null;
            drpType.DataBind();
            drpType.Items.Insert(0, new ListItem("Select Type", "0"));
        }
    }

    private void fillTeam(string TeamId)
    {
        team = new Team(_dataObject);
        team.Id = Convert.ToInt32(TeamId);
        team.GetTeamById();
        if (team.Id != 0)
        {
            txtTeamName.Text = team.TeamName;
            drpType.SelectedValue = Convert.ToString(team.Type);
            txtAbbrivation.Text = team.Abbrivation;
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        team = new Team(_dataObject);
        team.TeamName = txtTeamName.Text;
        team.Abbrivation = txtAbbrivation.Text;
        team.Type = drpType.SelectedValue;
        team.SeriesId = Convert.ToInt32(drpType.SelectedValue);
        bool teamNameExist = CheckTeamNameExist(txtTeamName.Text);
        bool abbrevationNameExist = CheckAbbrevationExist(txtAbbrivation.Text);
        team.IsActive = true;
        team.IsDeleted = false;
        if (Request.QueryString["Id"] != null)
        {
            if (teamNameExist)
            {
                layerError.InnerHtml = "<strong>Error!</strong> Team name already exist."; ;
                layerError.Attributes.Add("style", "display:block;");
                layerSuccess.Attributes.Add("style", "display:none;");
            }
            else if (abbrevationNameExist)
            {
                layerError.InnerHtml = "<strong>Error!</strong> Abbrevation already exist."; ;
                layerError.Attributes.Add("style", "display:block;");
                layerSuccess.Attributes.Add("style", "display:none;");
            }
            else
            {
                team.DateUpdated = System.DateTime.UtcNow;
                team.Id = Convert.ToInt32(Request.QueryString["Id"].ToString());
                team.UpdateTeam();
                Response.Redirect("ViewTeam.aspx");
            }
            
        }
        else
        {
            if (teamNameExist)
            {
                layerError.InnerHtml = "<strong>Error!</strong> Team name already exist."; ;
                layerError.Attributes.Add("style", "display:block;");
                layerSuccess.Attributes.Add("style", "display:none;");
            }
            else if (abbrevationNameExist)
            {
                layerError.InnerHtml = "<strong>Error!</strong> Abbrevation already exist."; ;
                layerError.Attributes.Add("style", "display:block;");
                layerSuccess.Attributes.Add("style", "display:none;");
            }
            else
            {
                team.DateAdded = System.DateTime.UtcNow;
                team.Create();
                layerError.Attributes.Add("style", "display:none;");

                layerSuccess.InnerHtml = "<strong>Success!</strong> Team added successfully.";
                layerSuccess.Attributes.Add("style", "display:block;");
                clearControl();
            }
        }
    }

    private void clearControl()
    {
        txtTeamName.Text = string.Empty;
        txtAbbrivation.Text = string.Empty;
        drpType.SelectedIndex = -1;
    }

    private bool CheckAbbrevationExist(string abbrevation)
    {
        bool isExist = false;
        int TeamId = 0;
        if (Convert.ToString(Request.QueryString["Id"]) != "" && Request.QueryString["Id"] != null)
        {
            TeamId = Convert.ToInt32(Request.QueryString["Id"]);
        }
        List<Team> teams = new List<Team>();
        teams = team.CheckAbbrivationExist(TeamId, abbrevation);
        if (teams.Count > 0)
        {
            isExist = true;
        }
        else
        {
            isExist = false;
        }
        return isExist;
    }

    private bool CheckTeamNameExist(string teamName)
    {
        bool isExist = false;
        int TeamId = 0;
        if (Convert.ToString(Request.QueryString["Id"]) != "" && Request.QueryString["Id"] != null)
        {
            TeamId = Convert.ToInt32(Request.QueryString["Id"]);
        }
        List<Team> teams = new List<Team>();
        teams = team.CheckTeamNameExist(TeamId, teamName);
        if (teams.Count > 0)
        {
            isExist = true;
        }
        else
        {
            isExist = false;
        }
        return isExist;
    }
    protected void btnView_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewTeam.aspx");
    }
}